
var Audience = {
    id: "AudienceTable",	//表格id
    seItem: null,//选中的条目
    AudienceData : {},
    table: null,
    layerIndex: -1
};



/**
 * 初始化表格的列
 */
Audience.initColumn = function () {
    return [
        {width:20,checkbox:true},
        {title: '序号',align : 'center',valign : 'middle',width:20,radio: false,checkbox:false,formatter:function (value, row, index) {  return index+1; } },
        {title: 'ID', field: 'id',visible: false, sortable: true, align: 'center', valign: 'middle'},
        {title: '性别', field: 'sex', width:120,visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '姓名', field: 'name', width:200,visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '公司名_中文', field: 'companyCn',width:200, visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '公司名_英文', field: 'companyEn',width:200, visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '职位', field: 'position', width:200,visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '部门', field: 'department',width:200, visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '国家', field: 'country',width:200, visible: false, sortable: true, align: 'center', valign: 'middle'},
        {title: '省', field: 'province', width:200,visible: false, sortable: true, align: 'center', valign: 'middle'},
        {title: '市', field: 'city',width:150, visible: false, sortable: true, align: 'center', valign: 'middle'},
        {title: '详细地址', field: 'address',width:150, visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '邮编', field: 'postcode', width:150,visible: false, sortable: true, align: 'center', valign: 'middle'},
        {title: '座机国家区号', field: 'seatCountryCode',width:80, visible: false, sortable: true, align: 'center', valign: 'middle'},
        {title: '座机地区号', field: 'seatAreaCode',width:120, visible: false, sortable: true, align: 'center', valign: 'middle'},
        {title: '座机号', field: 'seatcode',width:120, visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '传真国家区号', field: 'faxCountryCode', width:100,visible: false, sortable: true, align: 'center', valign: 'middle'},
        {title: '传真地区号', field: 'faxAreaCode',width:80, visible: false, sortable: true, align: 'center', valign: 'middle'},
        {title: '传真号', field: 'faxcode',width:80, visible: false, sortable: true, align: 'center', valign: 'middle'},
        {title: '手机区号', field: 'phoneAreaCode',width:120, visible: false, sortable: true, align: 'center', valign: 'middle'},
        {title: '手机号', field: 'phone',width:80, visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '邮箱', field: 'email',width:120, visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '网站', field: 'website',width:80, visible: false, sortable: true, align: 'center', valign: 'middle'},
        {title: '公司商业性质1', field: 'businessType1',width:150, visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '公司商业性质2', field: 'businessType2',width:150, visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '公司商业性质3', field: 'businessType3', width:150,visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '公司行业性质', field: 'industryType',width:80, visible: true, sortable: true, align: 'center', valign: 'middle'},
        {title: '客户身份', field: 'property',width:100, visible: true, sortable: true, align: 'center', valign: 'middle'},


        {title: '操作', field: '', width:120,align: 'center', valign: 'middle', sortable: true,formatter:Audience.editFormatter}
    ];
};
/**
 * 编辑列 自定义
 */
Audience.editFormatter=function (value, row, index){
    console.log(Authority.update)
    var btnArr =[];
    var btnDetail =  {clickEvent:'Audience.openDetail('+row.id+');',title:'详情',icon:HsetIcon['detail'],active:true};
    var btnUpdate =  {clickEvent:'Audience.openEditAudience('+row.id+');',title:'修改',icon:HsetIcon['update'],active:false};
    var btnDelete =  {clickEvent:'Audience.delete('+row.id+');',title:'删除',icon:HsetIcon['delete'],active:false};
    if(Authority.update==true){
        btnUpdate.active=true;
    }
    if(Authority.delete==true){
        btnDelete.active=true;
    }
    btnArr.push(btnDetail);
    btnArr.push(btnUpdate);
    btnArr.push(btnDelete);
    return HsetIcon.createGroup(btnArr);
};
/**
 * 打开查看展会管理详情
 */
Audience.openDetail = function (id) {
    Container.showRight();
    Container.setIframeSrc(Feng.ctxPath + '/audience/detail/'+id);
};
/**
 * 修改
 */
Audience.openEditAudience = function (id) {
    window.location.href=Feng.ctxPath+"/audience/audience_update/"+id;
};
/**
 * 新增
 */
Audience.openAddAudience = function () {
    window.location.href=Feng.ctxPath+"/audience/audience_add/";
};
/**
 * 删除
 */
Audience.delete = function (id) {
    Feng.confirm("确认要删除吗?",ensure);
    function ensure() {
        var ajax = new $ax(Feng.ctxPath + "/audience/delete/"+id, function (data) {
            Feng.success("删除成功!");
            Audience.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
};
/**
 * 检查是否选中(可多选)
 */
Audience.checks = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Audience.seItems = [];
        for(var i=0;i<selected.length;i++){
            console.log("id:"+selected[i].id);
            Audience.seItems.push(selected[i].id);
        }
        return true;
    }
};
/**
 * 设置对话框中的数据
 *
 * @param key
 *            数据的名称
 * @param val
 *            数据的具体值
 */
Audience.set = function(key, val) {
    this.AudienceData[key] = (typeof val == "undefined") ? $("#" + key).val(): val;
    console.log("AudienceData:"+this.AudienceData);
    return this;
};
/**
 * 收集数据
 */
Audience.collectData = function() {
    this.set('ids',Audience.seItems.join(','));
};
/**
 * 清除数据
 */
Audience.clearData = function() {
    this.AudienceData = {};
};
/**
 * 提交修改
 */
Audience.batchDelete = function() {
    if (Audience.checks()) {
    Feng.confirm('确认批量删除选中数据?',invoke);
    function invoke() {
        Audience.clearData();
        Audience.collectData();
            //提交信息
            var ajax = new $ax(Feng.ctxPath + "/audience/batchDelete", function (data) {
                Feng.success("删除成功!");
                //返回列表
                window.location.href = Feng.ctxPath + "/audience";
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set(this.Audience.AudienceData);
            ajax.start();
        }
    }
};
/**
 * 自定义按钮组
 */
Audience.btnGroup=function(){
    var arr=[];
    if(Authority['export']==true) {//导出
        arr.push({clickEvent: 'Audience.export();', title: '观众信息下载', icon: HsetIcon['download']});
    }
    if(Authority['import']==true) {//导入
        arr.push({clickEvent: 'Audience.import();', title: '导入信息', icon: HsetIcon['upload']});
    }
    if(Authority['add']==true) {//新增
        arr.push({clickEvent: 'Audience.openAddAudience();', title: '新增', icon: HsetIcon['add']});
    }
    if(Authority['batchDelete']==true) {//删除
        arr.push({clickEvent: 'Audience.batchDelete();', title: '删除', icon: HsetIcon['delete']});
    }
    return arr;
};
/**
 * 导入
 */
Audience.import = function () {
    var index = layer.open({
        type: 2,
        title: '导入',
        area: ['800px', '500px'], //宽高
        fix: false, //不固定
        maxmin: true,
        offset:'10px',
        content: Feng.ctxPath + '/audience/import'
    });
    this.layerIndex = index;
};

/**
 * 普通搜索 整合查询数据
 * @returns {{}}
 */
Audience.normalAudienceData=function(){
    //清空高级搜索 勿删！！
    Audience.highResetSearch();
    //初始为已清空的 高级搜索条件 即所有条件都为空
    var queryData =Audience.highAudienceData() ;
    // {搜索字段:搜索字段值}
    var values = $('#normalFieldName').val().split(';');
    for (var i = 0; i < values.length; i++) {
        queryData[values[i]] =  $.trim($('.normalSearchGroup [name="'+values[i]+'"]').val());
    }
    return queryData;
};
/**
 * 高级搜索 整合查询数据
 * @returns {{}}
 */
Audience.highAudienceData=function(){
    var queryData = {};
    $.each($('.highSearchGroup input'),function () {
        queryData[this.name] = $.trim(this.value);
    });
    $.each($('.highSearchGroup select'),function () {
        queryData[this.name] = $.trim(this.value);
    });
    return queryData;
};
/**
 * 普通搜索
 */
Audience.normalSearch = function () {
    Audience.table.refresh({query: Audience.normalAudienceData()});
};
/**
 * 高级搜索
 */
Audience.highSearch = function () {
    Audience.table.refresh({query: Audience.highAudienceData()});
};
/**
 * 高级搜索 清空
 */
Audience.highResetSearch = function () {
    //隐藏 input
    $.each($('.highSearchGroup input'),function () {
        $(this).val('');
    });
    //隐藏 select
    $.each($('.highSearchGroup select'),function () {
        $(this).val('');
        $(this).trigger("chosen:updated");
    });
};
/**
 * 导出excel
 */
Audience.export = function(){
    var exportParam ={};
    if($('.highSearch').css('display')=='none'){//普通搜索
        exportParam['data'] = Audience.normalAudienceData();
    }else{//高级搜索
        exportParam['data'] = Audience.highAudienceData();
    }
    exportParam['req'] = '/audience/export';
    VE.setParam(exportParam);
    VE.export('AudienceExport');
};
/**
 * 绑定事件
 */
Audience.bind=function(){
    //普通搜索字段change事件
    $('#normalFieldName').bind('change',function(){
        //隐藏 input
        $.each($('.normalSearchGroup input'),function () {
            $(this).val('');
            $(this).parents('.form-horizontal').eq(0).hide();
        });
        //隐藏 select
        $.each($('.normalSearchGroup select'),function () {
            $(this).val('');
            $(this).parents('.form-horizontal').eq(0).hide();
        });
        //显示当前搜索条件
        var values = this.value.split(';');
        for (var i = 0; i < values.length; i++) {
            $('.normalSearchGroup [name="'+values[i]+'"]').parents('.form-horizontal').eq(0).show();
        }
    });
};
$(function () {
    Audience.bind();
    var defaultColunms = Audience.initColumn();
    var table = new BSTable(Audience.id, "/audience/list", defaultColunms);
    table.setPaginationType("client");
    table.setBtnGroup(Audience.btnGroup());
    Audience.table = table.init();
});
